<?php

Yii::import('application.extensions.online_payment.online_payment');

class EpaymentController extends MemberController {

    public function actionPay() {

        $model = new epay_form;

        if ($_POST) {
            if ($model->validate()) {


                $produk = epay_products::model()->findByPk($_POST['epay_form']['transaction_products_id']);
                $kode_produk = $produk->products_code;
                $adm_fee = $produk->products_administration_fee;
                $id_pelanggan = $_POST['epay_form']['transaction_ppob_idpelanggan'];

                //INQUIRY DULU
                $inquiry = new online_payment();
                $respon = $inquiry->inquiry($kode_produk, $id_pelanggan);
                $respon['product_name'] = $produk->products_name;
                //fee dari system + fee dari netsol
                $fee = $adm_fee + $respon['jumlahadm'];
                //jumlah pengurangan ewallet
                $total_bayar = $respon['jumlahtagihan'] + $fee;

                Yii::app()->user->setFlash('idpelanggan', $respon['idpelanggan']);
                Yii::app()->user->setFlash('product_name', $respon['product_name']);
                Yii::app()->user->setFlash('namapelanggan', $respon['namapelanggan']);
                Yii::app()->user->setFlash('jumlahtagihan', $respon['jumlahtagihan']);
                Yii::app()->user->setFlash('jumlahadm', $respon['jumlahadm']);
                Yii::app()->user->setFlash('fee', $fee);
                Yii::app()->user->setFlash('jumlahbayar', $respon['jumlahbayar']);
                Yii::app()->user->setFlash('total_bayar', $total_bayar);
                Yii::app()->user->setFlash('produk', $respon['produk']);
                Yii::app()->user->setFlash('pesan', $respon['pesan']);
                
                Yii::app()->user->setFlash('product_id', $_POST['epay_form']['transaction_products_id']);

                //foreach ($respon as $key => $val) {
                //}
                //jangan lupa cek apakah ewallet cukup untuk membayar tagihan
                if (is_array($respon)) {
                    if ($respon['status'] == 'G') {
                        $model->addError('gagal', strtolower($respon['pesan']));
                        //die('gagal');
                    } elseif ($respon['status'] == 'S') {
                        $this->redirect('payment');
                        //print_r($respon);
                    } elseif ($respon['status'] == 'P') {
                        $model->addError('gagal', strtolower($respon['pesan']));
                    }
                }
            }
        }

        $this->render('pay', array(
            'model' => $model,
        ));
    }

    public function actionPayment() {

        $model = new epay_form;

        $produk = epay_products::model()->findByPk($_POST['product_id']);
        $kode_produk = $produk->products_code;
        $adm_fee = $produk->products_administration_fee;
        $id_pelanggan = $_POST['epay_form']['transaction_ppob_idpelanggan'];

        if ($_POST) {
            if ($model->validate()) {
                //validasi transaksinya disini saja

                $kode_produk = $_POST['transaction_ppob_produk'];
                $id_pelanggan = $_POST['transaction_ppob_idpelanggan'];
                $jumlah_bayar = $_POST['transaction_ppob_jumlahbayar'];
                $fee = $_POST['transaction_ppob_fee'];
                $total_bayar = $_POST['transaction_ppob_total_bayar'];

                //bayar tagihanya
                $payment = new online_payment;
                $payment = $payment->payment($kode_produk, $id_pelanggan, $jumlah_bayar);

                //masukan data log transaksinya
                $transaction = new epay_transaction;
                $transaction->transaction_network_id = Yii::app()->user->getId();
                $transaction->transaction_products_id = $_POST['product_id'];
                $transaction->transaction_ppob_refid = $payment['refid'];
                $transaction->transaction_ppob_produk = $payment['produk'];
                $transaction->transaction_ppob_idpelanggan = $payment['idpelanggan'];
                $transaction->transaction_ppob_namapelanggan = $payment['namapelanggan'];
                $transaction->transaction_ppob_jumlahtagihan = $payment['jumlahtagihan'];
                $transaction->transaction_ppob_jumlahadm = $fee;
                $transaction->transaction_ppob_jumlahbayar = $total_bayar;
                $transaction->transaction_ppob_refnumber = $payment['refnumber'];
                $transaction->transaction_ppob_pesan = $payment['pesan'];
                $transaction->transaction_ppob_status = $payment['status'];
                $transaction->transaction_ppob_code = $payment['code'];
                $transaction->transaction_ppob_rpcid = $payment['rpcid'];
                $transaction->transaction_datetime = date("Y-m-d H:i:s");
                $transaction->save();

                //masukan ke log ewallet
                $ewallet_log = new sys_ewallet_log;
                $ewallet_log->ewallet_log_network_id = Yii::app()->user->getId();
                $ewallet_log->ewallet_log_type = "cash";
                $ewallet_log->ewallet_log_value = $total_bayar;
                $ewallet_log->ewallet_log_status = "debit";
                $ewallet_log->ewallet_log_datetime = date("Y-m-d H:i:s");
                $ewallet_log->ewallet_log_note = "Payment Online";
                $ewallet_log->save();

                //kurangi ewalletnya
                $criteria = new CDbCriteria;
                $criteria->condition = "ewallet_network_id = '" . Yii::app()->user->getId() . "'";
                $ewallet = sys_ewallet::model()->find($criteria);
                $ewallet->ewallet_cash_paid = ( $ewallet->ewallet_cash_paid + $total_bayar);
                $ewallet->save();

                //flash respon
                $payment['product_name'] = $produk->products_name;
                Yii::app()->user->setFlash('idpelanggan', $payment['idpelanggan']);
                Yii::app()->user->setFlash('product_name', $payment['product_name']);
                Yii::app()->user->setFlash('namapelanggan', $payment['namapelanggan']);
                Yii::app()->user->setFlash('jumlahtagihan', $payment['jumlahtagihan']);
                Yii::app()->user->setFlash('jumlahadm', $payment['jumlahadm']);
                Yii::app()->user->setFlash('fee', $fee);
                Yii::app()->user->setFlash('jumlahbayar', $payment['jumlahbayar']);
                Yii::app()->user->setFlash('total_bayar', $total_bayar);
                Yii::app()->user->setFlash('produk', $payment['produk']);
                Yii::app()->user->setFlash('pesan', $payment['pesan']);

                if ($payment['status'] == 'G') {
                    $this->addError('gagal', $payment['pesan']);
                } elseif ($payment['status'] == 'S' OR $payment['status'] == 'P') {
                    $this->redirect('success');
                }
            }
        }

        $this->render('payment', array(
            'model' => $model,
        ));
    }

    public function actionSuccess() {

        $model = new epay_form;


        $this->render('success', array(
            'model' => $model,
        ));
    }

    public function actionLog() {
        $date_start_default = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") - 7, date("Y")));
        $date_end_default = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") - 1, date("Y")));

        $date_start = (!empty($_POST['date_start'])) ? $_POST['date_start'] : $date_start_default;
        $date_end = (!empty($_POST['date_end'])) ? $_POST['date_end'] : $date_end_default;

        $criteria = new CDbCriteria;

        $criteria->condition = "transaction_network_id = '" . Yii::app()->user->getId() . "'";
        $criteria->condition = "transaction_datetime >= '$date_start'";
        $criteria->condition = "transaction_datetime <= '$date_end'";
        $criteria->order = "transaction_datetime ASC";

        $model = epay_transaction::model()->with("rel_epay_products")->findAll($criteria);

        $this->render('log', array(
            'model' => $model,
            'date_start' => $date_start,
            'date_end' => $date_end,
        ));
    }

}
